#include "PhotoViewer.h"
#include "ui_PhotoViewer.h"

PhotoViewer::PhotoViewer(QPixmap pixmap, QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::PhotoViewer)
    , m_pixmap(pixmap)
{
    ui->setupUi(this);
    m_scene = new QGraphicsScene(this);
    ui->graphicsView->setRenderHint(QPainter::Antialiasing);
    ui->graphicsView->setRenderHint(QPainter::SmoothPixmapTransform);
    ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    m_photoItem = new QGraphicsPixmapItem(pixmap);
    m_photoItem->setFlag(QGraphicsItem::ItemIsMovable, true);// 允许平移
    m_scene->addItem(m_photoItem);
    m_scene->setSceneRect(m_photoItem->boundingRect());
    ui->graphicsView->setScene(m_scene);

    // 设置旋转和缩放的中心为图像的几何中心
    m_photoItem->setTransformOriginPoint(pixmap.width() / 2, pixmap.height() / 2);
}

PhotoViewer::~PhotoViewer()
{
    delete ui;
}

void PhotoViewer::on_rotateBtn_clicked()
{
    m_photoItem->setRotation(m_photoItem->rotation() + 90);
}


void PhotoViewer::on_biggerBtn_clicked()
{
    double factor = 1.2;// 比例
    QTransform transform;
    transform.scale(factor, factor);
    m_photoItem->setTransform(transform, true);
}


void PhotoViewer::on_smallerBtn_clicked()
{
    double factor = 0.8;// 比例
    QTransform transform;
    transform.scale(factor, factor);
    m_photoItem->setTransform(transform, true);
}

